<template>
  <a-drawer
    v-model:visible="isDrawer"
    :width="width"
    :title="title"
    unmountOnClose
    placement="right"
    ok-text="提交"
    :ok-loading="subLoading"
    :on-before-ok="formSubmit"
    @cancel="handleReset"
  >
    <a-form
      :model="formParams"
      :rules="rules"
      ref="formRef"
      label-placement="left"
      :label-width="80"
    >
      <a-form-item label="配置名称" field="configName">
        <a-input placeholder="请输入配置名称" v-model="formParams.configName" />
      </a-form-item>
      <a-form-item label="配置编码" field="configCode">
        <a-input placeholder="请输入配置编码" v-model="formParams.configCode" />
      </a-form-item>
      <a-form-item label="配置值" field="configValue">
        <a-input placeholder="请输入配置值" v-model="formParams.configValue" />
      </a-form-item>
      <a-form-item label="描述" field="configDesc">
        <a-textarea placeholder="请输入配置描述" v-model="formParams.configDesc" />
      </a-form-item>
      <a-form-item label="排序" field="configOrder">
        <a-input-number placeholder="显示排序" v-model="formParams.configOrder" />
      </a-form-item>
    </a-form>
  </a-drawer>
</template>

<script lang="ts" setup>
  import { ref, onMounted } from 'vue';
  import { Message } from '@arco-design/web-vue';
  import type { formParamsType } from './types';

  import { addConfig, editConfig, configInfo } from '@/api/system/config';

  const rules = {
    configName: {
      required: true,
      message: '配置名称不能为空',
      trigger: 'blur',
    },
    configCode: {
      required: true,
      message: '配置编码不能为空',
      trigger: 'blur',
    },
    configValue: {
      required: true,
      message: '配置值不能为空',
      trigger: 'blur',
    },
  };

  const emit = defineEmits(['change']);

  defineProps({
    title: {
      type: String,
      default: '添加配置',
    },
    width: {
      type: Number,
      default: 450,
    },
    permissionList: {
      type: Array,
    },
  });

  const defaultValueRef = () => ({
    id: undefined,
    configCode: '',
    configDesc: '',
    configName: '',
    configOrder: undefined,
    configValue: '',
  });

  const formRef: any = ref(null);
  const isDrawer = ref(false);
  const subLoading = ref(false);

  const formParams = ref<formParamsType>(defaultValueRef());

  function openDrawer(id?) {
    if (id) {
      formParams.value.id = id;
      getInfo();
      return;
    }
    isDrawer.value = true;
  }

  function closeDrawer() {
    isDrawer.value = false;
  }

  function formSubmit(done) {
    formRef.value.validate((errors) => {
      if (errors) {
        Message.error('请填写完整信息');
        subLoading.value = false;
        return done(false);
      }
      if (formParams.value.id) {
        editConfig(formParams.value)
          .then((_) => {
            Message.success('编辑成功');
            emit('change');
            handleReset();
            return done(true);
          })
          .catch(() => {
            return done(false);
          });
      } else {
        addConfig(formParams.value)
          .then((_) => {
            Message.success('添加成功');
            emit('change');
            handleReset();
            return done(true);
          })
          .catch(() => {
            return done(false);
          });
      }
    });
  }

  function handleReset() {
    formRef.value.resetFields();
    formParams.value = Object.assign(formParams.value, defaultValueRef());
  }

  function getInfo() {
    configInfo({ id: formParams.value.id }).then((res) => {
      const info = {
        id: res.id,
        configCode: res.configCode,
        configDesc: res.configDesc,
        configName: res.configName,
        configOrder: res.configOrder,
        configValue: res.configValue,
      };
      formParams.value = info;
      isDrawer.value = true;
    });
  }

  onMounted(() => {});

  defineExpose({
    openDrawer,
    closeDrawer,
  });
</script>
